function idat = fsb_normalize_noise(idat,sandbox)

% FSB : use the noise outside of the brain to normalize the images
%
% EXAMPLE:
% idat = fsb_normalize_noise(idat,sandbox)
%
% INPUT:
% idat:         4-D image information
% sandbox:      sandbox experiment struct
%
% OUTPUT:
% idat:         normalized 4D image information
% 
% CALLED BY:
% FSB.m
%
% NOTES:
% Takes the noise timecourse to normalize the brain signal.
% Useful to see what signal remains after noise is removed by
% normalization.
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
% 
%$ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dim = size(idat); % use dimensions of conc_idat
noise = sandbox.art_mean_x; 
noisemean = mean(noise);
noise= noise/noisemean; % normalize to 1

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Calculate a 4-D matrix containing the correction values (faster than
% for-next loops)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
corr2 = repmat(noise,[1 dim(1) dim(2) dim(3)]);
corr3 = shiftdim(corr2,1);

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%Change the original matrix to double to allow for division
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
idat = single(idat);
idat = idat./corr3;
idat = int16(idat);

end

